package com.fmjava.service_video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fmjava.service_video.entity.Content;
import com.fmjava.service_video.entity.Description;
import com.fmjava.service_video.entity.vo.ContentPreviewVo;
import com.fmjava.service_video.entity.vo.ContentVo;
import com.fmjava.service_video.mapper.ContentMapper;
import com.fmjava.service_video.service.ChapterService;
import com.fmjava.service_video.service.ContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fmjava.service_video.service.DescriptionService;
import com.fmjava.service_video.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 作品表 服务实现类
 * </p>
 *
 * @author fmjava
 * @since 2021-07-19
 */
@Service
@Transactional
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements ContentService {

    @Autowired
    ChapterService chapterService;

    @Autowired
    DescriptionService descriptionService;

    @Autowired
    VideoService videoService;
    @Override
    public String insertContent(ContentVo contentVo) {
        Content content = new Content();
        BeanUtils.copyProperties(contentVo, content);
        baseMapper.insert(content);

        String id = content.getId();
        Description description = new Description();
        description.setId(id);
        description.setDescription(contentVo.getDescription());
        descriptionService.save(description);
        return id;

    }

    @Override
    public ContentVo getOneContent(String id) {
        //1、获取content
        ContentVo contentVo = new ContentVo();
        Content content = baseMapper.selectById(id);
        BeanUtils.copyProperties(content,contentVo);

        //2、获取description
        Description description = descriptionService.getById(id);
        contentVo.setDescription(description.getDescription());
        return contentVo;
    }

    @Override
    public void updateContent(ContentVo contentVo) {
        Content content = new Content();
        BeanUtils.copyProperties(contentVo, content);
        baseMapper.updateById(content);

        Description description = new Description();
        BeanUtils.copyProperties(contentVo, description);
        descriptionService.updateById(description);
    }

    @Override
    public ContentPreviewVo getContentView(String id) {
        return baseMapper.getContentView(id);
    }

    @Override
    public void getContentList(Page<Content> pageContent, ContentVo contentVo) {
        QueryWrapper<Content> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (contentVo == null){
            baseMapper.selectPage(pageContent, queryWrapper);
            return;
        }
        if (!StringUtils.isEmpty(contentVo.getTitle())) {
            queryWrapper.like("title", contentVo.getTitle());
        }
        if (!StringUtils.isEmpty(contentVo.getAuthorId()) ) {
            queryWrapper.eq("author_id", contentVo.getAuthorId());
        }
        if (!StringUtils.isEmpty(contentVo.getCategoryParentId())) {
            queryWrapper.eq("category_parent_id", contentVo.getCategoryParentId());
        }
        if (!StringUtils.isEmpty(contentVo.getCategoryId())) {
            queryWrapper.eq("category_id", contentVo.getCategoryId());
        }
        baseMapper.selectPage(pageContent,queryWrapper);
    }

    @Override
    public void deleteContent(String id) {
        //1、删除视频
        videoService.deleteVideo(id);
        //2、删除章节
        chapterService.deleteChapter(id);
        //4、删除描述
        descriptionService.deleteContent(id);
        //3、删除作品信息
        baseMapper.deleteById(id);
    }
}
